import { getToken } from "@/assets/js/auth";
import axios from "axios";
import { Message } from "element-ui";
import Cookies from "js-cookie";
import qs from 'qs';
import { clearCookie } from "@/assets/js/auth";
import router from "@/router";

const service = axios.create({
    //   baseURL: process.env.VUE_APP_BASE_API, // 从环境变量读取基础地址
    baseURL: '/api',
    timeout: 30000 // 请求超时时间
})

// 请求拦截器
service.interceptors.request.use(
    config => {
        // 在发送请求前做些什么
        // if(localStorage.getItem('token')) {
        //     config.headers['token'] = `${localStorage.getItem('token')}`
        // }
        return config
    },
    error => {
        // 对请求错误做些什么
        return Promise.reject(error)
    }
)

// 响应拦截器
service.interceptors.response.use(
    res => {
        if (res.data.code === 401) {
            router.push("/");
            clearCookie("employee");
            clearCookie("token");
        }
        return res;
    },
    error => {
        // return Promise.reject(error);
        console.log(error)
    }
);

/*刷新Cooke的存活时间*/
function refreshCookies() {
    var token=getToken()
    if (token){
        //刷新cookie
        service({
            url:"/checkedToken",
            method:"GET",
            params:{token:token}
        }).then(res=>{
            res=res.data

            if (res.code==200){
                Cookies.set("token", res.data.token, {expires: 1/48})
                Cookies.set("employee", JSON.stringify(res.data.employee), {expires: 1/48})
            }

        }).catch((e)=>{
            console.log(e)
        })
    }
    return token;

}

/*post请求*/
export function ajaxPost(url, param) {
    return service({
        url: url,
        method: "POST",
        headers:{
            'token': refreshCookies() //设置token 其中K名要和后端协调好
        },
        data: qs.stringify(param)
    })
}

/*json请求*/
export function ajaxJson(url, param) {
    return service({
        url: url,
        method: "POST",
        headers:{
            'Content-Type':'application/json',
            'token': refreshCookies() //设置token 其中K名要和后端协调好
        },
        data: param
    })
}

/*GET请求*/
export function ajaxGet(url, param) {
    return service({
        url: url,
        method: "GET",
        headers: {
            'token': refreshCookies() //设置token 其中K名要和后端协调好
        },
        params:param
    })
}

export function post(url, param) {
    return service({
        url: url,
        method: "POST",
        headers:{
            'Content-Type':'application/json',
            'token': refreshCookies() //设置token 其中K名要和后端协调好
        },
        data: param
    })
}

export function deleteFor(url) {
    return service({
        url: url,
        method: "DELETE",
        headers:{
            'Content-Type':'application/json',
            'token': refreshCookies() //设置token 其中K名要和后端协调好
        }
    })
}

/*给用户弹出相应的提示*/
export function popup(msg, type) {
    switch (type) {
        case "warning":
            Message.warning(msg);
            break;
        case "error":
            Message.error({message: msg, duration: 5000, showClose: true});
            break;
        case "info":
            Message.info(msg);
            break;
        default:
            Message.success(msg);
    }

}